#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<sys/types.h>
#include<unistd.h>
#include<vector>
#include<cstdlib>
#include<sys/wait.h>
using namespace std;
#define COMMAND_SIZE 1024
#define MAXARGV 128
#define PROMPT "[%s@%s %s]# " 
char* g_argv[MAXARGV];
int g_argc=0;
void Print()
{
    printf(PROMPT,getenv("USER"),getenv("HOSTNAME"),getenv("PWD"));
}
bool GetCommand(char out[],int size)
{
    char* c=fgets(out,size,stdin);
    if(c==NULL) return false;
    out[strlen(out)-1]=0;
    if(strlen(out)==0) return false;
    else return true;
}
void AnalyseCommand(char out[])
{
#define SYMBOL " "
    g_argc=0;
    g_argv[g_argc++]=strtok(out,SYMBOL);
    while((bool)(g_argv[g_argc++]=strtok(NULL,SYMBOL)));
    g_argv[g_argc]=0;
}
void ForkChild()
{
    pid_t id=fork();
    if(id==0)
    {
        execvp(g_argv[0],g_argv);
        exit(1);
    }
    pid_t p=waitpid(id,nullptr,0);
    (void)p;
}
int main()
{
    while(1)
    {
        //命令行提示打印
        Print();
        //获取用户输入命令
        char out[COMMAND_SIZE];
        GetCommand(out,sizeof(out));
        //命令解析
        AnalyseCommand(out);
        //int i=0;
       //for(;i<g_argc;i++)
           // std::cout<<g_argv[i]<<' ';
        //命令执行
        ForkChild();
    }
    return 0;
}
